package com.cbw.filter;

import com.cbw.util.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter({"/admin/index.html","/admin/views/*","/express/*"})
public class AccessControllerFilter implements Filter {
    @Override
    public void init(FilterConfig config) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response=(HttpServletResponse)resp;
        String userName = UserUtil.getUserName(request.getSession());
        System.out.println("过滤器session_usernmae:"+userName);
        if (userName!=null){
            //管理员登陆过,放行
            filterChain.doFilter(req,resp);
        }else {
            ((HttpServletResponse) resp).sendError(404,"很遗憾，权限不足(您尚未登录)");
        }
    }

    @Override
    public void destroy() {

    }
}
